package logic

import (
	"C/api_C/internal/pkg"
	"C/renrenzhuC_srv/renrenzhuC"
	"context"

	"C/api_C/internal/svc"
	"C/api_C/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.RenrenzhuC_Srv.UserLogin(l.ctx, &renrenzhuC.UserLoginRequest{
		Tel:      req.Tel,
		Password: req.Password,
		Send:     req.Send,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "用户登录失败",
		}, nil
	}
	token, err := pkg.GetJwtToken(login.UserId)
	if err != nil {
		return nil, err
	}
	return &types.Response{
		Code: 200,
		Msg:  "用户登录成功",
		Data: map[string]interface{}{
			"token": token,
		},
	}, nil
}
